package tick;

import java.util.ArrayList;
import java.util.Iterator;

public class TickManager {
	
	private static TickManager instance = new TickManager();
	
	private ArrayList<ITick> lst = new ArrayList<ITick>();
	
	private long date;
	
	private TickManager()
	{
		date = System.currentTimeMillis();
	}
	
	/**
	 * 注册一个Tick
	 * @param tick
	 * @return
	 */
	public boolean enroll(ITick tick)
	{
		if(!lst.contains(tick))
		{
			return lst.add(tick);
		}
		return false;
	}
	
	/**
	 * 注销Tick
	 * @param tick
	 * @return
	 */
	public boolean logout(ITick tick)
	{
		return lst.remove(tick);
	}
	
	public void extuce() 
	{
		long nowTime = System.currentTimeMillis();
		int time = (int) (nowTime - date);
		Iterator<ITick> iterator = lst.iterator();
		while (iterator.hasNext()) 
		{
			ITick tick =  iterator.next();
			tick.extuce(time);
		}
		date = nowTime;
	}
	
	public static TickManager getInstance()
	{
		return instance;
	}

}
